package code.tools.commponents
{
	import code.structure.manager.loader.DllLoaderManager;
	import code.structure.manager.font.FontManager;
	import code.structure.module.pop.view.Windows;
	import code.untils.FiltersEnum;
	import code.untils.UiNameConst;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormatAlign;

	/**
	 *基础面板 (包含背景+标题+描述+关闭按钮)
	 * @author liangqian
	 * 
	 */	
	public class BasePanel extends Windows
	{
		private static const DESC_GLOW_FILTER:GlowFilter = new GlowFilter(0xffffff,1,2,2,8);
		/** 背景  */
		protected var _bg:Sprite;
		protected var _container:Sprite;
		private var titleBm:Bitmap;
		/** 标题遮罩  */
		private var _titleShade:Sprite;
		/** 标题文字 */
		private var _titleTf:TextField;
		/** 描述文字  */
		private var _descTf:TextField;
		private var _closeCallBack:Function;
		
		private var _width:int;
		private var _height:int;
		private var _bgXpos:int;
		private var _bgYpos:int;
		protected var _bgWidth:Number;
		protected var _bgHeight:Number;
		private var _isShowCloseBtn:Boolean;
		public function BasePanel(width:int,height:int,bgWidth:Number,bgHeight:Number,bgXpos:int = 0,bgYpos:int = 0,
								  closeCallBack:Function = null,isShowCloseBtn:Boolean = true)
		{
			_width = width;
			_height = height;
			_bgWidth = bgWidth;
			_bgHeight = bgHeight;
			_bgXpos = bgXpos;
			_bgYpos = bgYpos;
			_closeCallBack = closeCallBack;
			_isShowCloseBtn = isShowCloseBtn;
			super();
		}
	
		override protected function initializePanel():void
		{
			super.initializePanel();
			
			/* 背景  */
			_bg = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.PANEL_VIEW,'panel_bg')as Class)as Sprite;
			_bg.width = _bgWidth;
			_bg.height = _bgHeight;
			_bg.x = _bgXpos;
			_bg.y = _bgYpos;
			_mMain.addChild(_bg);
			_bg.filters = [FiltersEnum.PANEL_DROP_SHADOW_FILTER];
			
			_container = new Sprite();
			_mMain.addChild(_container);
			/* 标题遮罩  */
			_titleShade =  new (DllLoaderManager.getInstance().getMaterial(UiNameConst.PANEL_VIEW,'title_shade')as Class)as Sprite;
			_titleShade.width = width;
			_titleShade.y = 12;
			_mMain.addChild(_titleShade);
			/* 标题文字 */
			_titleTf = Helpers.FormatTextField(null,45,0,500,TextFieldAutoSize.CENTER,0x794828,FontManager.getInstance().embeddedFontBName,34);
			(_titleTf as AutoTextField).openZoom(500,40);
			/* 描述文字  */
			_descTf = Helpers.FormatTextField(_mMain,10,62,_bgWidth - 20,TextFieldAutoSize.CENTER,0x666666,FontManager.getInstance().embeddedFontAName,14);
			(_descTf as AutoTextField).openZoom(_bgWidth - 20,20);
			//_descTf.wordWrap = true;
			_descTf.filters = [DESC_GLOW_FILTER];
			_titleTf.mouseEnabled = _descTf.mouseEnabled = false;
			
			if(_isShowCloseBtn)
				new CloseBtn(_mMain,_bg.x + _bg.width - 55,13,closeHandler);
			
			appearFactory(4,4);
		}
		
		override public function get height():Number
		{
			return _height;
		}
		
		override public function get width():Number
		{
			return _width;
		}
		
		
		public function set title(t:String):void
		{
			_titleTf.text = t;
			titleBm = Helpers.drawConvertBm(titleBm,_titleTf);
			titleBm.cacheAsBitmap = true;
//			_titleShade.y = _titleYpos + 6;
//			titleBm.y = _titleYpos;
			_titleShade.y = 25;
			titleBm.y = 20;
			titleBm.x = _bg.x + (_bgWidth - titleBm.width)*.5;
			_titleShade.filters = [FiltersEnum.TITLE_GLOW_FILTER,FiltersEnum.TITLE_DROP_SHADOW_FILTER];
			_mMain.addChild(titleBm);
			_titleShade.mask = titleBm;
		}
		
		private var _titleYpos:int = 12;
		public function set titleYpos(ypos:Number):void
		{
			_titleYpos = ypos;
		}
		
		public function set desc(d:String):void
		{
			_descTf.text = d;
		}
		
		protected function closeHandler(...args):void
		{
			disappear();
			if(_closeCallBack != null)
				_closeCallBack();
		}
	}
}